using System;

namespace Beetle.Protocol;

public abstract class SizePackage : Package
{
	private new class a
	{
		public bool a = true;

		public int b = -1;

		private int m_c;

		public byte[] d = new byte[4];

		public void c(byte A_0)
		{
			d[this.m_c] = A_0;
			if (this.m_c == 3)
			{
				b = BitConverter.ToInt32(d, 0);
				if (!a)
				{
					b = j.a(b);
				}
			}
			else
			{
				this.m_c++;
			}
		}

		public void c()
		{
			b = -1;
			this.m_c = 0;
		}
	}

	private new bool m_a;

	private new a b;

	private ReadObjectInfo c = new ReadObjectInfo();

	protected WriteBlock mWriteBlock = new WriteBlock();

	public SizePackage()
	{
	}

	public SizePackage(IChannel channel)
		: base(channel)
	{
	}

	public override void Import(byte[] data, int start, int count)
	{
		if (b == null)
		{
			b = new a();
			b.a = base.LittleEndian;
		}
		while (count > 0)
		{
			if (!this.m_a)
			{
				b.c();
				((x)Writer).Reset();
				this.m_a = true;
			}
			if (b.b == -1)
			{
				while (count > 0 && b.b == -1)
				{
					b.c(data[start]);
					start++;
					count--;
				}
				if (b.b > TcpUtils.DataPacketMaxLength)
				{
					throw NetTcpException.DataOverflow();
				}
				if (b.b > 0)
				{
					Writer.Write(b.d, 0, 4);
				}
			}
			else if (a(data, ref start, ref count))
			{
				if (count > 0)
				{
					BufferOffset = start;
					BufferCount = count;
				}
				else
				{
					BufferOffset = 0;
					BufferCount = 0;
				}
				this.m_a = false;
				Writer.Position = 0L;
				OnMessageDataReader(Writer);
			}
		}
	}

	private bool a(byte[] A_0, ref int A_1, ref int A_2)
	{
		if (A_2 >= b.b)
		{
			Writer.Write(A_0, A_1, b.b);
			A_1 += b.b;
			A_2 -= b.b;
			return true;
		}
		Writer.Write(A_0, A_1, A_2);
		A_1 += A_2;
		b.b -= A_2;
		A_2 = 0;
		return false;
	}

	protected virtual void OnReadHeader(IDataReader reader)
	{
		reader.ReadInt();
	}

	protected virtual void ReadBody(IDataReader reader, IMessage message)
	{
		message.Load(reader);
	}

	public override IMessage MessageRead(IDataReader reader)
	{
		OnReadHeader(reader);
		IMessage message = null;
		message = ReadMessageByType(reader, c);
		if (message == null)
		{
			throw NetTcpException.TypeNotFound(c.ToString());
		}
		try
		{
			ReadBody(reader, message);
			return message;
		}
		catch (Exception innerexception)
		{
			NetTcpException ex = NetTcpException.ObjectLoadError(c.ToString(), innerexception);
			throw ex;
		}
	}

	protected virtual void WriteBody(IDataWriter writer, IMessage msg)
	{
		msg.Save(writer);
	}

	public override void MessageWrite(IMessage msg, IDataWriter writer)
	{
		int num = (int)writer.Length;
		WriteBlock writeBlock = ((x)writer).Allocation4Byte(mWriteBlock);
		writeBlock.LittleEndian = base.LittleEndian;
		WriteMessageType(msg, writer);
		WriteBody(writer, msg);
		int data = (int)writer.Length - num - 4;
		writeBlock.SetData(data);
	}

	protected abstract IMessage ReadMessageByType(IDataReader reader, ReadObjectInfo typeTag);

	protected abstract void WriteMessageType(IMessage msg, IDataWriter writer);

	protected override void OnDisposed()
	{
		base.OnDisposed();
	}
}
